package com.bnlr.lrparking.appui.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.HorizontalAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.model.CarM;
import com.bnlr.lrparking.model.VerifyCodeM;
import com.bnlr.lrparking.util.CountDownTimerUtils;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.shizhefei.view.indicator.Indicator;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.ColorBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by hp on 2017/8/15.
 * 在线申诉
 */

public class ComplainActivity extends BaseActivity{
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.complain_scrollIndicatorView)
    ScrollIndicatorView scrollIndicatorView;
    private List<String> list = new ArrayList<>();
    @BindView(R.id.complain_one)
    LinearLayout complain_one;
    @BindView(R.id.complain_two)
    LinearLayout complain_two;
    @BindView(R.id.complain_three)
    RelativeLayout complain_three;
    @BindView(R.id.tv_next_btn)
    TextView tv_next_btn;
    @BindView(R.id.btn_submit)
    TextView btn_submit;
    @BindView(R.id.tv_ok)
    TextView tv_ok;
    @BindView(R.id.tv_verifica)
    TextView tvCode;
    @BindView(R.id.et_email)
    EditText etEmail;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_ID)
    EditText etID;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_wx)
    EditText etWx;
    private boolean isClick=false;
    private Unbinder unbinder;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complain_one);
        unbinder = ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        tvNavTitle.setText("在线申诉");
        list.add("1.填写邮件");
        list.add("2.填写申诉资料");
        list.add("3.完成");
        tvCode.setOnClickListener(this);
        tv_next_btn.setOnClickListener(this);
        btn_submit.setOnClickListener(this);
        tv_ok.setOnClickListener(this);
        set(scrollIndicatorView,list);
        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (RegexUtils.isEmail(s.toString())) {
                    tvCode.setEnabled(true);
                    if (!TextUtils.isEmpty(etCode.getText().toString())) {
                        tv_next_btn.setEnabled(true);
                    }
                } else {
                    tvCode.setEnabled(false);
                    tv_next_btn.setEnabled(false);
                }
            }
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(etCode.getText().toString()) && !TextUtils.isEmpty(etEmail.getText().toString())) {
                    tv_next_btn.setEnabled(true);
                } else {
                    tv_next_btn.setEnabled(false);
                }
            }
        });
    }



    private void set(final Indicator indicator, List list) {
        int selectColor = getResources().getColor(R.color.main_color);
        int unSelectColor = getResources().getColor(R.color.text_color);
        indicator.setAdapter(new HorizontalAdapter(this, list));
        indicator.setScrollBar(new ColorBar(getApplicationContext(), selectColor, 4));
        float unSelectSize = 13;
        float selectSize = unSelectSize * 1f;
        indicator.getItemView(1).setMinimumWidth(30);
        indicator.setOnTransitionListener(new OnTransitionTextListener().setColor(selectColor, unSelectColor).setSize(selectSize, unSelectSize));
        indicator.setItemClickable(false);
        indicator.setOnItemSelectListener(new Indicator.OnItemSelectedListener() {
            @Override
            public void onItemSelected(View selectItemView, int select, int preSelect) {
                if (isClick == true) {
                    if (select == 0) {
                        complain_one.setVisibility(View.VISIBLE);
                        complain_two.setVisibility(View.GONE);
                        complain_three.setVisibility(View.GONE);
                    } else if (select == 1) {
                        complain_one.setVisibility(View.GONE);
                        complain_two.setVisibility(View.VISIBLE);
                        complain_three.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_verifica:
                String email = etEmail.getText().toString();
                SendYzm(email);
                break;
            case R.id.tv_next_btn:
                String code = etCode.getText().toString();
                String email1 = etEmail.getText().toString();
                VerityEmail(code,email1);
                tv_next_btn.setEnabled(false);
                break;
            case R.id.btn_submit:
                String id = etID.getText().toString();
                String phone = etPhone.getText().toString();
                String psw = etPsw.getText().toString();
                String wx = etWx.getText().toString();
                if (TextUtils.isEmpty(id) || TextUtils.isEmpty(phone)) {
                    ToastUtils.showShort("请补全信息");
                    return;
                }
                if (!RegexUtils.isMobileExact(phone)) {
                    ToastUtils.showShort("手机格式不正确");
                    return;
                }
                submit(id,phone,psw,wx);

                break;
            case R.id.tv_ok:
                finish();
                isClick=false;
                break;
        }
    }

    private void submit(String id, String phone, String psw, String wx) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "SubmitComplain");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("idCard", id);
            mRequest.add("phone", phone);
            if (psw != null) {
                mRequest.add("oldPwd", psw);
            }
            if (wx != null) {
                mRequest.add("wxNumber", wx);
            }
            mRequest.add("code", etCode.getText().toString());
            mRequest.add("email", etEmail.getText().toString());
            // 添加到请求队列
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<CarM>(this, true, CarM.class) {
                        @Override
                        public void doWork(CarM data, String code) {
                            if ("2".equals(data.getMsgcode())) {

                            } else {
                                ToastUtils.showShort(data.getMsg());
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("2".equals(obj.getString("msgcode"))) {
                                    scrollIndicatorView.setCurrentItem(2, true);
                                    complain_one.setVisibility(View.GONE);
                                    complain_two.setVisibility(View.GONE);
                                    complain_three.setVisibility(View.VISIBLE);
                                } else {
                                    CommonUtil.showToask(ComplainActivity.this, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void VerityEmail(String code, String email1) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "ValidComplainEmail");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("code", code);
            mRequest.add("email", email1);
            // 添加到请求队列
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<CarM>(this, true, CarM.class) {
                        @Override
                        public void doWork(CarM data, String code) {
                            if ("1".equals(code)) {
                                isClick=true;
                                scrollIndicatorView.setCurrentItem(1, true);
                                complain_one.setVisibility(View.GONE);
                                complain_two.setVisibility(View.VISIBLE);
                                complain_three.setVisibility(View.GONE);
                            } else {
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                tv_next_btn.setEnabled(true);
                                if (!"1".equals(code)) {
                                    ToastUtils.showShort(obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }
    private void SendYzm(String email) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "SendComplainMail");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("email", email);
            // 添加到请求队列
            CallServer.getRequestInstance().add(this, mRequest,
                    new CustomHttpListener<VerifyCodeM>(this, true, VerifyCodeM.class) {
                        @Override
                        public void doWork(VerifyCodeM data, String code) {
                        }
                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {//{"msgcode":"1","msg":"成功","data":{"code":"06641"}}
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    setVerifica(tvCode);
                                } else {
                                    CommonUtil.showToask(ComplainActivity.this, obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    private void setVerifica(TextView tvCode) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvCode, 60000, 1000);
        mCountDownTimerUtils.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}
